package com.zmht.beike.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.TCaiminfo;
import com.zmht.beike.po.TCmonthinfo;
import com.zmht.beike.po.TCweekcontent;
import com.zmht.specedu.vo.Teacher;

public class CweekcontentAction extends BaseAction {

	/*
	 * (non-Javadoc) 初始化周内容添加页面
	 * 
	 * @see org.apache.struts.actions.DispatchAction#execute(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward getWeekContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Teacher teacher = (Teacher) request.getSession().getAttribute("current_user_session_key");
		String week = request.getParameter("week");
		TCmonthinfo monthinfo = (TCmonthinfo)request.getSession().getAttribute("cmonthinfo");
		if(monthinfo==null){
			List<TCmonthinfo> list = this.getCweekinfoBiz().findByHQL("from TCmonthinfo t where t.id="+request.getParameter("cmonthId"));
			monthinfo = list.get(0);
		}
		String weekThemeName = "";
		// 开始分页查询
		Integer index = 1;
		Integer pageSize = 5;
		if (null != request.getParameter("currPage")) {
			index = new Integer(request.getParameter("currPage"));
		}
		if (null != request.getParameter("pageSize")) {
			pageSize = new Integer(request.getParameter("pageSize"));
		}

		List<TCweekcontent> list = this.getCweekcontentBiz().getCweekContents(teacher.getTeacherId(),new Integer(week),monthinfo.getMonth(),(index - 1)*pageSize,pageSize);
		int total = this.getCweekcontentBiz().getCweekContentTotal(teacher.getTeacherId(),new Integer(week),monthinfo.getMonth());
	
		
		request.setAttribute("currPage", index);
		request.setAttribute("pageAmount", (int) Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("week", week);
		
		if(list!=null&&list.size()>0){weekThemeName = list.get(0).getWeekThemeName();}
		request.setAttribute("weekThemeName", weekThemeName);
		request.setAttribute("weekcontents", list);
		request.setAttribute("month", monthinfo.getMonth());
		request.setAttribute("targetId", monthinfo.getCaimId());
		request.setAttribute("areaId", monthinfo.getCareaId());
		return mapping.findForward("zhouzhuti_content");
	}
	/**
	 * 添加内容
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addWeekContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			Teacher teacher = (Teacher) request.getSession().getAttribute("current_user_session_key");
			TCaiminfo caiminfo = (TCaiminfo)request.getSession().getAttribute("caiminfo");
			//TCmonthinfo monthinfo = (TCmonthinfo)request.getSession().getAttribute("cmonthinfo");
			String month = request.getParameter("month");
			if(null==caiminfo){
				String targetId = request.getParameter("targetId");
				caiminfo = this.getCaiminfoBiz().findById(new Integer(targetId));
			}
			
			String week = request.getParameter("week");
			String weekThemeName = request.getParameter("weekThemeName");
			TCweekcontent content = new TCweekcontent();
			
			content.setWeek(new Integer(week));
			content.setCweekContent(request.getParameter("cweekContent"));
			content.setTeacherId(teacher.getTeacherId());
			content.setWeekThemeName(weekThemeName);
			content.setCaimId(caiminfo.getId());
			content.setMonth(new Integer(month));
			content.setCareaId(caiminfo.getAreaId());
			
			String areaId = request.getParameter("areaId");
			if(null!=areaId&&!"".equals(areaId)&&!"".equals("null")){
				content.setCareaId(new Integer(areaId));
			}
			
			content.setTerm(caiminfo.getTerm());
			
			this.getCweekcontentBiz().saveCweekContent(content);
			
			if("1".equals(request.getParameter("edit"))){
			
				return new ActionForward("/beike/cweekcontent.do?method=getWeekContentEdit&week="+week+"&month="+month+"&targetId="+caiminfo.getId()+"&areaId="+areaId);
			}
			
		
		return new ActionForward("/beike/cweekcontent.do?method=getWeekContent&week="+week);

	}
	/**
	 * 删除
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delWeekContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("cwid");
		String week = request.getParameter("week");
		String month = request.getParameter("month");
		String targetId = request.getParameter("targetId");
		String areaId = request.getParameter("areaId");
		this.getCweekcontentBiz().deleteCweekContent(new Integer(id));
		
		
		if("1".equals(request.getParameter("edit"))){
			
			return new ActionForward("/beike/cweekcontent.do?method=getWeekContentEdit&week="+week+"&month="+month+"&targetId="+targetId+"&areaId="+areaId);
		}
		return new ActionForward("/beike/cweekcontent.do?method=getWeekContent&week="+week);
		

	} 
	/**
	 * 更新 周内容记录
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateWeekContent(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = request.getParameter("cid");
		String week = request.getParameter("week");
		String cweekContent = request.getParameter("cweekContent");
		String month = request.getParameter("month");
		String targetId = request.getParameter("targetId");
		TCweekcontent content = this.getCweekcontentBiz().findById(new Integer(id));
		
		if(content!=null){
			content.setCweekContent(cweekContent);
			this.getCweekcontentBiz().updateCweekContent(content);
		}

		if("1".equals(request.getParameter("edit"))){
			String areaId = request.getParameter("areaId");
			return new ActionForward("/beike/cweekcontent.do?method=getWeekContentEdit&week="+week+"&month="+month+"&targetId="+targetId+"&areaId="+areaId);
		}
		return new ActionForward("/beike/cweekcontent.do?method=getWeekContent&week="+week);
		

	} 
	/**
	 * 编辑目标时调用
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getWeekContentEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Teacher teacher = (Teacher) request.getSession().getAttribute("current_user_session_key");
		String week = request.getParameter("week");
		String month = request.getParameter("month");
		String targetId = request.getParameter("targetId");
		String areaId = request.getParameter("areaId");
		
		Integer careaId = 0;
		if(null!=areaId&&!"".equals(areaId)){
			careaId = new Integer(areaId);
		}
		//System.out.println("careaId =========="+careaId+"week="+week+"  month="+month+" targetid = "+targetId+" areaId="+areaId);
		//TCmonthinfo monthinfo = (TCmonthinfo)request.getSession().getAttribute("cmonthinfo");
		
		String weekThemeName = "";
		// 开始分页查询
		Integer index = 1;
		Integer pageSize = 5;
		if (null != request.getParameter("currPage")) {
			index = new Integer(request.getParameter("currPage"));
		}
		if (null != request.getParameter("pageSize")) {
			pageSize = new Integer(request.getParameter("pageSize"));
		}

		//List<TCweekcontent> list = this.getCweekcontentBiz().getCweekContents(teacher.getTeacherId(),new Integer(week),monthinfo.getMonth(),(index - 1)*pageSize,pageSize);
		//int total = this.getCweekcontentBiz().getCweekContentTotal(teacher.getTeacherId(),new Integer(week),monthinfo.getMonth());
		List<TCweekcontent> list = this.getCweekcontentBiz().getCweekContentsEdit(new Integer(targetId), teacher.getTeacherId(),careaId, new Integer(month), new Integer(week), (index - 1)*pageSize, pageSize);
		int total = this.getCweekcontentBiz().getCweekContentsEditTotal(new Integer(targetId), teacher.getTeacherId(),careaId, new Integer(month), new Integer(week));
		
		//System.out.println("==========size========"+list.size()+"=====total="+total);
		request.setAttribute("currPage", index);
		request.setAttribute("pageAmount", (int) Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
	
		
		if(list!=null&&list.size()>0){weekThemeName = list.get(0).getWeekThemeName();}
		request.setAttribute("weekThemeName", weekThemeName);
		request.setAttribute("weekcontents", list);
		
		request.setAttribute("targetId", targetId);
		request.setAttribute("month", month);
		request.setAttribute("week", week);
		request.setAttribute("areaId", areaId);
		request.setAttribute("edit", "1");
		return mapping.findForward("zhouzhuti_content");
	}
}
